// ignore_for_file: must_be_immutable

import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/ems/rectangle_ems_romote_control/rectangle_control_instruct/rectangle_control_instruct_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'rectangle_control_instruct_controller.dart';

class RectangleControlInstructWidget
    extends GetView<RectangleControlInstructController> {
  int? deviceId;

  RectangleControlInstructWidget({
    super.key,
    required this.deviceId,
  });

  @override
  final controller = Get.put(RectangleControlInstructController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RectangleControlInstructController>(
      builder: (_) {
        return SizedBox(
          width: double.infinity,
          // height: controller.model.canReset ? 405 : 303,
          child: _listBody(),
        );
      },
    );
  }

  Widget _listBody() {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext context, int index) {
        final RectangleControlInstructItem item =
            controller.model.instructList(controller.productId)[index];

        return Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          width: double.infinity,
          height: 50,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                child: Text(
                  item.label,
                  style: TextStyle(
                    fontSize: index == 0 ? 18.sp : 16.sp,
                    fontWeight:
                        index == 0 ? FontWeight.w500 : FontWeight.normal,
                  ),
                ),
              ),
              index == 0
                  ? Container()
                  : GetBuilder<RectangleControlInstructController>(
                      builder: (_) => CupertinoSwitch(
                        value: item.open,
                        dragStartBehavior: DragStartBehavior.down,
                        activeColor: Colour.primaryColor,
                        onChanged: (value) {
                          controller.pushSwitch(item, value);
                          _showDialog(context, item, index, value);
                        },
                      ),
                    ),
            ],
          ),
        );
      },
      itemCount: controller.model.instructList(controller.productId).length,
      separatorBuilder: (BuildContext context, int index) {
        return index !=
                controller.model.instructList(controller.productId).length - 1
            ? Divider(
                color: Colour.dividerColor,
                height: 1,
                indent: 16.w,
                endIndent: 16.w,
              )
            : Container();
      },
    );
  }

  void _showDialog(BuildContext context, item, int index, bool isPress) {
    BrnDialogManager.showConfirmDialog(
      context,
      title: "",
      cancel: ResKey.cancel.tr,
      confirm: ResKey.sure.tr,
      message: controller.judgeDialogText(index),
      barrierDismissible: false,
      themeData: BrnDialogConfig(
        contentTextStyle: BrnTextStyle(
          color: Colour.mainTextColor,
          fontSize: 18.sp,
          fontWeight: FontWeight.w500,
        ),
        contentPaddingSm: EdgeInsets.symmetric(
          horizontal: 42.w,
        ),
        contentPaddingLg: EdgeInsets.symmetric(
          horizontal: 42.w,
        ),
      ),
      onConfirm: () {
        isPress = !isPress;
        controller.pushSwitch(item, isPress);
        var params = controller.collectParams(item, controller.deviceId);
        Log.d('params: $params');
        controller.sendCommand(context, params);
        Get.back();
      },
      onCancel: () {
        isPress = !isPress;
        controller.pushSwitch(item, isPress);
        Get.back();
      },
    );
  }
}
